import store from '../store';

// 验证token
export const validToken = (token) => {
  let isToken = false;
  if (token) {
    if (new Date().getTime() < store.state.overdueTime * 1000) {
      isToken = true;
    } else {
      // 清除登录信息
      store.dispatch('commitClearUserData');
      isToken = false;
    }
  } else {
    store.dispatch('commitClearUserData');
    isToken = false;
  }
  return isToken;
};
// 接口基础路径
export const baseUrl = '/sys';
// 公钥
export const publicKey =
  '04F805B414D9737E6CC1BBBBE468394DE0A7393A39C419AACE2DC5E5F530F1B021BBCC5C25DCC16A6B2FE735ED6F25B7BB476AEEECCAC4BF88CCEBF7C07E65AAC8';
// 文件下载路径
export const downloadUrl = baseUrl + '/download/file?fileName=';
// 手机号正则
export const mobileReg = /^1[3456789]\d{9}$/;
// 邮箱正则
export const emailReg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
// input type=number的长度限制
export const limitNumberLen = (val, form, option, limit) => {
  form[option] = val.length > limit ? val.slice(0, limit) : val;
};
//需要post请求讲参数放入请求头时
export const postType = (obj) => {
  let newPara = '?';
  for (let key in obj) {
    newPara += key + '=' + obj[key] + '&';
  }
  return newPara.substr(0, newPara.length - 1);
};
// 分页列表删除当页数据后的回调处理
export const deleteListItem = (total, pageSize, currentPage, callback) => {
  let newCurrentPage;
  const totalPage = Math.ceil(total / pageSize);
  if (totalPage === 0) {
    newCurrentPage = 1;
  } else {
    newCurrentPage = currentPage > totalPage ? totalPage : currentPage;
  }
  callback(newCurrentPage);
};
// 获取随机字符串
export const getRandomString = (len) => {
  len = len || 32;
  const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  const maxPos = chars.length;
  let pwd = '';
  for (let i = 0; i < len; i++) {
    pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
};
// 获取文件后缀名
export const getSuffix = (filename) => {
  const index = filename.lastIndexOf('.');
  return index !== -1 ? filename.substring(index) : '';
};
// 复制获取区域信息
export const getAreaList = () => {
  const areaList = store.state.systemConfig.areaList;
  return JSON.parse(JSON.stringify(areaList));
};
// 表格头部
export const tabHeader = function () {
  return 'backgroundColor:#D3E8FE;color:#121432;text-align:center;height:60px;padding:0;font-size:14px;font-weight:600';
};
